import requests

def get_video_msg(videoId, signature):
    cookies = {
        'STUDY_SESS': '"4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyOi5CeCKf99g6sKGDiCpYc4ZMvchdl9Chs/SyjOz6DduZaJyZFTEJ9ZFKzmhgLFB8jN/V+deEWRSVY7inlnu17YjaraaOe2FU8kdG7YcfGCracjGdT1Py8NCvVP06WSRmgLhur2Nm2wEb9HcEikV+3FTI8+lZKyHhiycNQo+g+/oA=="',
        'STUDY_INFO': '"yd.e76de41718584d74a@163.com|8|1445910011|1648773195575"',
        'DICT_SESS': 'v2|hQwWZzFDbBwLnfUlO4qF0wFkLYWRHOM0eu0fkAhLwuRYfhMqukMeK0kfRHw4hfkf0YMO4UfhLYY0Uf6LzGOLOA0Yl0HeuRMkWR',
        'DICT_LOGIN': '1||1648773195634',
    }
    headers = {
        'authority': 'vod.study.163.com',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="99", "Microsoft Edge";v="99"',
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
        'sec-ch-ua-platform': '"Windows"',
        'content-type': 'application/x-www-form-urlencoded',
        'accept': '*/*',
        'origin': 'https://www.icourse163.org',
        'sec-fetch-site': 'cross-site',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://www.icourse163.org/',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        # Requests sorts cookies= alphabetically
        # 'cookie': 'STUDY_SESS="4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyOi5CeCKf99g6sKGDiCpYc4ZMvchdl9Chs/SyjOz6DduZaJyZFTEJ9ZFKzmhgLFB8jN/V+deEWRSVY7inlnu17YjaraaOe2FU8kdG7YcfGCracjGdT1Py8NCvVP06WSRmgLhur2Nm2wEb9HcEikV+3FTI8+lZKyHhiycNQo+g+/oA=="; STUDY_INFO="yd.e76de41718584d74a@163.com|8|1445910011|1648773195575"; DICT_SESS=v2|hQwWZzFDbBwLnfUlO4qF0wFkLYWRHOM0eu0fkAhLwuRYfhMqukMeK0kfRHw4hfkf0YMO4UfhLYY0Uf6LzGOLOA0Yl0HeuRMkWR; DICT_LOGIN=1||1648773195634',
    }
    params = {
        'videoId': videoId,
        'signature': signature,
        'clientType': '1',
    }
    response = requests.get('https://vod.study.163.com/eds/api/v1/vod/video', headers=headers, params=params,
                            cookies=cookies)
    return response.json()
def get_signature(bizId):
    cookies = {
        'EDUWEBDEVICE': '4c609a9df5dd4e5e88f98a9f58680303',
        'hb_MA-A976-948FFA05E931_source': 'cn.bing.com',
        'WM_TID': 'TkaeKpELohRFVQRRAFY%2B6pYmTTm%2F%2FgQw',
        '__yadk_uid': 'IxIjbqlIDW7Wjo3t7fLoqAQzAmQXh8q9',
        'NTESSTUDYSI': '2f886e5098024f358c32c2b471896569',
        'Hm_lvt_77dc9a9d49448cf5e629e5bebaa5500b': '1647474835,1648773114',
        'WM_NI': 'CuSp6OgUnoHjDh7Jjh77%2FM%2FZkGSzii7wrirjCTypaL0H%2F9FpmP5Glkeszo5TV3NDa4zObSGta4BW7E2dMw2IvjlzVyZ7M82B4MtAN2q5xF0zzgtK4r2ASda3PcqSoNWzeTY%3D',
        'WM_NIKE': '9ca17ae2e6ffcda170e2e6eeb9c648ad97af8ee734a9eb8ba2d45a938b8eaab67aedb88788d952b68daa9bf22af0fea7c3b92a81ab9d91b545a58ba290cb6288b8f9abe964f5e9e1aeae52a9999d9bec63f5a9be8af162818cb791e448f498e1dac57caf90fba8ef61a7adb690b86096ed82a5e440ba91aeb4c680a3ec8eb8b64ff2bc8398b143b6b6a49bd5739ae983d9b14af893bca2cd3ab3ebb8a7b372bab9bcacb77af29f9d85e43994bdbfb4ee4695a6abb5dc37e2a3',
        'STUDY_WTR': '"4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyMdRWtf+qAyuBNDd7hzY/aXQuOYHl2InUpRsrOcL0c6wkKjr+lQK52Ujy4oooqcvTw="',
        'STUDY_INFO': '"yd.e76de41718584d74a@163.com|8|1445910011|1648773195466"',
        'STUDY_SESS': '"4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyOi5CeCKf99g6sKGDiCpYc4ZMvchdl9Chs/SyjOz6DduVj2MzoyRRguDajcgRkhdxrcWy9ai7H2CMFo//hihTV39O+N3itFNzZqnlM0WCfn3M0NVISZSVtMw07WJccupBQLhur2Nm2wEb9HcEikV+3FTI8+lZKyHhiycNQo+g+/oA=="',
        'STUDY_PERSIST': '"k9ZX0GFKnD/8+ohORt3rKckAQBFdQW5J1fPd85SVkb4Xh+7IiciO/QXSLOJWjt6ZDVMXdmEjfeLRSu0KLfKLT7Bglj5I8CmxFqaOq880FjdVQG7w5/9VOI+mwg5ufB3f5l21Ksy3Yuu8B8iH/cyPEErGbDx177hh7H2pyn5sMSsnMktdS+U8oCeamBNWrVKNZ1HFybguQbfTwaxKIbGkqw09x9wJWDZuZYd4jE6zxXrZgpjCC7Iso4RP9U87vJE8LtaQzUT1ovP2MqtW5+L3Hw+PvH8+tZRDonbf7gEH7JU="',
        'NETEASE_WDA_UID': '1445910011#|#1602781128324',
        'CLIENT_IP': '222.209.79.185',
        'videoVolume': '0.8',
        'hasVolume': 'true',
        'utm': '"eyJjIjoiIiwiY3QiOiIiLCJpIjoiIiwibSI6IiIsInMiOiIiLCJ0IjoiIn0=|aHR0cHM6Ly9jbi5iaW5nLmNvbS8="',
        'Hm_lpvt_77dc9a9d49448cf5e629e5bebaa5500b': '1648789172',
    }
    headers = {
        'authority': 'www.icourse163.org',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="99", "Microsoft Edge";v="99"',
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
        'sec-ch-ua-platform': '"Windows"',
        'accept': '*/*',
        'origin': 'https://www.icourse163.org',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://www.icourse163.org/learn/STA-1449270163?tid=1465269461',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        # Requests sorts cookies= alphabetically
        # 'cookie': 'EDUWEBDEVICE=4c609a9df5dd4e5e88f98a9f58680303; hb_MA-A976-948FFA05E931_source=cn.bing.com; WM_TID=TkaeKpELohRFVQRRAFY%2B6pYmTTm%2F%2FgQw; __yadk_uid=IxIjbqlIDW7Wjo3t7fLoqAQzAmQXh8q9; NTESSTUDYSI=2f886e5098024f358c32c2b471896569; Hm_lvt_77dc9a9d49448cf5e629e5bebaa5500b=1647474835,1648773114; WM_NI=CuSp6OgUnoHjDh7Jjh77%2FM%2FZkGSzii7wrirjCTypaL0H%2F9FpmP5Glkeszo5TV3NDa4zObSGta4BW7E2dMw2IvjlzVyZ7M82B4MtAN2q5xF0zzgtK4r2ASda3PcqSoNWzeTY%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeb9c648ad97af8ee734a9eb8ba2d45a938b8eaab67aedb88788d952b68daa9bf22af0fea7c3b92a81ab9d91b545a58ba290cb6288b8f9abe964f5e9e1aeae52a9999d9bec63f5a9be8af162818cb791e448f498e1dac57caf90fba8ef61a7adb690b86096ed82a5e440ba91aeb4c680a3ec8eb8b64ff2bc8398b143b6b6a49bd5739ae983d9b14af893bca2cd3ab3ebb8a7b372bab9bcacb77af29f9d85e43994bdbfb4ee4695a6abb5dc37e2a3; STUDY_WTR="4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyMdRWtf+qAyuBNDd7hzY/aXQuOYHl2InUpRsrOcL0c6wkKjr+lQK52Ujy4oooqcvTw="; STUDY_INFO="yd.e76de41718584d74a@163.com|8|1445910011|1648773195466"; STUDY_SESS="4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyOi5CeCKf99g6sKGDiCpYc4ZMvchdl9Chs/SyjOz6DduVj2MzoyRRguDajcgRkhdxrcWy9ai7H2CMFo//hihTV39O+N3itFNzZqnlM0WCfn3M0NVISZSVtMw07WJccupBQLhur2Nm2wEb9HcEikV+3FTI8+lZKyHhiycNQo+g+/oA=="; STUDY_PERSIST="k9ZX0GFKnD/8+ohORt3rKckAQBFdQW5J1fPd85SVkb4Xh+7IiciO/QXSLOJWjt6ZDVMXdmEjfeLRSu0KLfKLT7Bglj5I8CmxFqaOq880FjdVQG7w5/9VOI+mwg5ufB3f5l21Ksy3Yuu8B8iH/cyPEErGbDx177hh7H2pyn5sMSsnMktdS+U8oCeamBNWrVKNZ1HFybguQbfTwaxKIbGkqw09x9wJWDZuZYd4jE6zxXrZgpjCC7Iso4RP9U87vJE8LtaQzUT1ovP2MqtW5+L3Hw+PvH8+tZRDonbf7gEH7JU="; NETEASE_WDA_UID=1445910011#|#1602781128324; CLIENT_IP=222.209.79.185; videoVolume=0.8; hasVolume=true; utm="eyJjIjoiIiwiY3QiOiIiLCJpIjoiIiwibSI6IiIsInMiOiIiLCJ0IjoiIn0=|aHR0cHM6Ly9jbi5iaW5nLmNvbS8="; Hm_lpvt_77dc9a9d49448cf5e629e5bebaa5500b=1648789172',
    }
    params = {
        'csrfKey': '2f886e5098024f358c32c2b471896569',
    }
    data = {
        'bizId': bizId,
        'bizType': '1',
        'contentType': '1',
    }
    response = requests.post('https://www.icourse163.org/web/j/resourceRpcBean.getResourceToken.rpc', headers=headers,
                             params=params, cookies=cookies, data=data)

    d = response.json()['result']['videoSignDto']
    return d['signature'], d['videoId']
def get_bizId(termId):
    cookies = {
        'EDUWEBDEVICE': '4c609a9df5dd4e5e88f98a9f58680303',
        'hb_MA-A976-948FFA05E931_source': 'cn.bing.com',
        'WM_TID': 'TkaeKpELohRFVQRRAFY%2B6pYmTTm%2F%2FgQw',
        '__yadk_uid': 'IxIjbqlIDW7Wjo3t7fLoqAQzAmQXh8q9',
        'NTESSTUDYSI': '2f886e5098024f358c32c2b471896569',
        'Hm_lvt_77dc9a9d49448cf5e629e5bebaa5500b': '1647474835,1648773114',
        'WM_NI': 'CuSp6OgUnoHjDh7Jjh77%2FM%2FZkGSzii7wrirjCTypaL0H%2F9FpmP5Glkeszo5TV3NDa4zObSGta4BW7E2dMw2IvjlzVyZ7M82B4MtAN2q5xF0zzgtK4r2ASda3PcqSoNWzeTY%3D',
        'WM_NIKE': '9ca17ae2e6ffcda170e2e6eeb9c648ad97af8ee734a9eb8ba2d45a938b8eaab67aedb88788d952b68daa9bf22af0fea7c3b92a81ab9d91b545a58ba290cb6288b8f9abe964f5e9e1aeae52a9999d9bec63f5a9be8af162818cb791e448f498e1dac57caf90fba8ef61a7adb690b86096ed82a5e440ba91aeb4c680a3ec8eb8b64ff2bc8398b143b6b6a49bd5739ae983d9b14af893bca2cd3ab3ebb8a7b372bab9bcacb77af29f9d85e43994bdbfb4ee4695a6abb5dc37e2a3',
        'STUDY_WTR': '"4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyMdRWtf+qAyuBNDd7hzY/aXQuOYHl2InUpRsrOcL0c6wkKjr+lQK52Ujy4oooqcvTw="',
        'STUDY_INFO': '"yd.e76de41718584d74a@163.com|8|1445910011|1648773195466"',
        'STUDY_SESS': '"4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyOi5CeCKf99g6sKGDiCpYc4ZMvchdl9Chs/SyjOz6DduVj2MzoyRRguDajcgRkhdxrcWy9ai7H2CMFo//hihTV39O+N3itFNzZqnlM0WCfn3M0NVISZSVtMw07WJccupBQLhur2Nm2wEb9HcEikV+3FTI8+lZKyHhiycNQo+g+/oA=="',
        'STUDY_PERSIST': '"k9ZX0GFKnD/8+ohORt3rKckAQBFdQW5J1fPd85SVkb4Xh+7IiciO/QXSLOJWjt6ZDVMXdmEjfeLRSu0KLfKLT7Bglj5I8CmxFqaOq880FjdVQG7w5/9VOI+mwg5ufB3f5l21Ksy3Yuu8B8iH/cyPEErGbDx177hh7H2pyn5sMSsnMktdS+U8oCeamBNWrVKNZ1HFybguQbfTwaxKIbGkqw09x9wJWDZuZYd4jE6zxXrZgpjCC7Iso4RP9U87vJE8LtaQzUT1ovP2MqtW5+L3Hw+PvH8+tZRDonbf7gEH7JU="',
        'NETEASE_WDA_UID': '1445910011#|#1602781128324',
        'CLIENT_IP': '222.209.79.185',
        'videoVolume': '0.8',
        'hasVolume': 'true',
        'utm': '"eyJjIjoiIiwiY3QiOiIiLCJpIjoiIiwibSI6IiIsInMiOiIiLCJ0IjoiIn0=|aHR0cHM6Ly9jbi5iaW5nLmNvbS8="',
        'Hm_lpvt_77dc9a9d49448cf5e629e5bebaa5500b': '1648789589',
    }
    headers = {
        'authority': 'www.icourse163.org',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="99", "Microsoft Edge";v="99"',
        'sec-ch-ua-mobile': '?0',
        'edu-script-token': '2f886e5098024f358c32c2b471896569',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
        'sec-ch-ua-platform': '"Windows"',
        'accept': '*/*',
        'origin': 'https://www.icourse163.org',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://www.icourse163.org/learn/STA-1449270163?tid=1465269461',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        # Requests sorts cookies= alphabetically
        # 'cookie': 'EDUWEBDEVICE=4c609a9df5dd4e5e88f98a9f58680303; hb_MA-A976-948FFA05E931_source=cn.bing.com; WM_TID=TkaeKpELohRFVQRRAFY%2B6pYmTTm%2F%2FgQw; __yadk_uid=IxIjbqlIDW7Wjo3t7fLoqAQzAmQXh8q9; NTESSTUDYSI=2f886e5098024f358c32c2b471896569; Hm_lvt_77dc9a9d49448cf5e629e5bebaa5500b=1647474835,1648773114; WM_NI=CuSp6OgUnoHjDh7Jjh77%2FM%2FZkGSzii7wrirjCTypaL0H%2F9FpmP5Glkeszo5TV3NDa4zObSGta4BW7E2dMw2IvjlzVyZ7M82B4MtAN2q5xF0zzgtK4r2ASda3PcqSoNWzeTY%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeb9c648ad97af8ee734a9eb8ba2d45a938b8eaab67aedb88788d952b68daa9bf22af0fea7c3b92a81ab9d91b545a58ba290cb6288b8f9abe964f5e9e1aeae52a9999d9bec63f5a9be8af162818cb791e448f498e1dac57caf90fba8ef61a7adb690b86096ed82a5e440ba91aeb4c680a3ec8eb8b64ff2bc8398b143b6b6a49bd5739ae983d9b14af893bca2cd3ab3ebb8a7b372bab9bcacb77af29f9d85e43994bdbfb4ee4695a6abb5dc37e2a3; STUDY_WTR="4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyMdRWtf+qAyuBNDd7hzY/aXQuOYHl2InUpRsrOcL0c6wkKjr+lQK52Ujy4oooqcvTw="; STUDY_INFO="yd.e76de41718584d74a@163.com|8|1445910011|1648773195466"; STUDY_SESS="4a/ppj7PhQYgZe9O6c0MOTEgPDKEHhBI6Y2a2g8YfyOi5CeCKf99g6sKGDiCpYc4ZMvchdl9Chs/SyjOz6DduVj2MzoyRRguDajcgRkhdxrcWy9ai7H2CMFo//hihTV39O+N3itFNzZqnlM0WCfn3M0NVISZSVtMw07WJccupBQLhur2Nm2wEb9HcEikV+3FTI8+lZKyHhiycNQo+g+/oA=="; STUDY_PERSIST="k9ZX0GFKnD/8+ohORt3rKckAQBFdQW5J1fPd85SVkb4Xh+7IiciO/QXSLOJWjt6ZDVMXdmEjfeLRSu0KLfKLT7Bglj5I8CmxFqaOq880FjdVQG7w5/9VOI+mwg5ufB3f5l21Ksy3Yuu8B8iH/cyPEErGbDx177hh7H2pyn5sMSsnMktdS+U8oCeamBNWrVKNZ1HFybguQbfTwaxKIbGkqw09x9wJWDZuZYd4jE6zxXrZgpjCC7Iso4RP9U87vJE8LtaQzUT1ovP2MqtW5+L3Hw+PvH8+tZRDonbf7gEH7JU="; NETEASE_WDA_UID=1445910011#|#1602781128324; CLIENT_IP=222.209.79.185; videoVolume=0.8; hasVolume=true; utm="eyJjIjoiIiwiY3QiOiIiLCJpIjoiIiwibSI6IiIsInMiOiIiLCJ0IjoiIn0=|aHR0cHM6Ly9jbi5iaW5nLmNvbS8="; Hm_lpvt_77dc9a9d49448cf5e629e5bebaa5500b=1648789589',
    }
    params = {
        'csrfKey': '2f886e5098024f358c32c2b471896569',
    }
    data = {
        'termId': termId,
    }
    response = requests.post('https://www.icourse163.org/web/j/courseBean.getLastLearnedMocTermDto.rpc',
                             headers=headers, params=params, cookies=cookies, data=data)
    video_d_ls = []
    for i in response.json()['result']['mocTermDto']['chapters']:
        chapter = i['name']
        lessons_ls = i['lessons']
        try:
            for j in lessons_ls:
                units_ls = j['units']
                for u in units_ls:
                    videomsgd = {
                        'chapter': chapter,
                        'name'   : u['name'],
                        'bizId'     : u['id']
                    }
                    video_d_ls.append(videomsgd)
        except TypeError:
            pass
    return video_d_ls

if __name__ == '__main__':
    termId = '1464315461' # 课程号
    class_ls = []
    for video_d in get_bizId(termId):
        try:
            signature, videoId = get_signature(video_d['bizId'])
            msg = get_video_msg(videoId, signature)
            class_ls.append({'chapter':video_d['chapter'], 'name': video_d['name'],'m3u8Url':msg['result']['videos'][0]['videoUrl']})
        except TypeError:
            print('None')
    print(class_ls)
    with open('term-{}.txt'.format(termId), 'w', encoding='utf-8') as fp:
        for i in class_ls:
            fp.write(str(i) + "\n")